<template>
  <div class="layout-all">
    <control :config="config" />
    <div class="layout-header" style="float:left">
      <!-- <router-link to="/barter_abt/barter/form">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-left:0px"
        >添加</el-button>
      </router-link> -->
    </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%;margin-top:50px"
      >
        <el-table-column align="center" prop="title" label="商品名" width="250" />
        <el-table-column align="center" prop="price" label="价格" width="250" />
        <el-table-column align="center" prop="thumb" label="封面" width="100">
          <template slot-scope="scope">
            <img :src="scope.row.thumb"  width="50px" height="50px">
          </template>
        </el-table-column>
        <el-table-column align="center" prop="cate_t1" label="商品分类" width="120">
          <template slot-scope="scope">
            <el-tag
              type="primary"
              disable-transitions
            >{{ scope.row.cate_t1 ? scope.row.cate_t1.title : '无' }} / {{ scope.row.cate_t2 ? scope.row.cate_t2.title : '无' }} </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="status" label="是否启用" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              size="mini"
            >{{ scope.row.status === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="status_str" label="状态" width="150">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.status_str === '待审核'"
              type="success"
              disable-transitions
            >{{ scope.row.status_str }}</el-tag>

            <el-tag
              v-if="scope.row.status_str === '审核通过'"
              type="info"
              disable-transitions
            >{{ scope.row.status_str }}</el-tag>
            <el-tag
              v-if="scope.row.status_str === '审核未通过'"
              type="danger"
              disable-transitions
            >{{ scope.row.status_str }}</el-tag>

            <el-tag
              v-if="scope.row.status_str === '待易货'"
              type="warning"
              disable-transitions
            >{{ scope.row.status_str }}</el-tag>

            <el-tag
              v-if="scope.row.status_str === '已易货'"
              disable-transitions
            >{{ scope.row.status_str }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="desc" label="描述" />
        <el-table-column
          align="center"
          prop="telephone"
          fixed="right"
          width="240"
          label="操作"
        >
          <template slot-scope="scope">
            <!-- <router-link :to="'/barter_abt/barter/form?id=' + scope.row.id">
              <el-button type="primary" icon="el-icon-edit" />
            </router-link> -->
            <el-button
              v-if="scope.row.is_audit === 1"
              type="primary"
              size="small"
              @click="onCheck(scope.row.id,1)"
            >
              审核
            </el-button>
            <el-button
            size="small"
              v-if="scope.row.is_audit === 0"
              type="danger"
              @click="onCheck(scope.row.id,2)"
            >
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
   layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      title="原因"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="remark" label="备注:">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入原因"
            maxlength="50"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onIsCheck()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import control from '../../components/control/control'
export default {
  components: { control },
  data() {
    return {
      config: [
        { type: 'text', placeholder: '请输入商品名称', field: 'title', val: '' }
      ],
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      total: 0,
      current_page: 1,
      per_page: 14,
      dialogVisible: false,
      form: {
        id: 0,
        status: 2,
        remark: ''
      },
      rules: {
        remark: [
          { required: true, message: '请输入原因', trigger: 'blur' }
        ]
      }
    }
  },
  activated(){
    // if(this.$getUpdate()){
      this.onSearch()
    // }
  },
  created() {
    this.onSearch()
  },
  methods: {
    // 查询
    handleSizeChange(val) {
      this.onSearch()
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      form.map = this.searchWhere
      this.get('/admin/GoodsBarter/index', form, 'POST').then(e => {
        this.tableData = e.list.data
        this.per_page = e.list.per_page
        this.total = e.list.total
        this.current_page = e.list.current_page
      })
    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      console.log(id)
    },
    // 弹框
    onCheck(id, status) {
      this.form.id = id
      this.form.status = status
      this.dialogVisible = true
    },
    // 审核或拒绝
    onIsCheck() {
      this.get('/admin/GoodsBarter/audit', this.form, 'POST').then(e => {
        this.form.remark = ''
        this.$msg_ok('success', '操作成功')
        this.dialogVisible = false
        this.onSearch()
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
</style>>
